﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.SessionState;
using System.Security.Principal;
using BLL;
using Entity;

namespace Web.admin.OutterManage
{
    public partial class AddOutterReport : System.Web.UI.Page
    {
        ContestService ContestService = new ContestService();
        CompetitionService competitionService = new CompetitionService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CompetitionList.DataSource = competitionService.getOuterCompetitions();
                CompetitionList.DataTextField = "COM_NAME";
                CompetitionList.DataValueField = "COM_ID";
                CompetitionList.DataBind();

                string operation = Request["OPERATION"];
                if (operation == null)
                {
                    AddContest();
                }
                string ContestId = Request["id"];
                if (ContestId == null)
                    return;

                if (operation == "Modify")
                {
                    ModifyContest(ContestId);
                }
                else if (operation == "Delete")
                {
                    DeleteContest(ContestId);
                }
            }
        }

        protected void AddContest()
        {
            IPrincipal user = HttpContext.Current.User;
            if (!(user is SitePrincipal))
                return;

            SitePrincipal sp = user as SitePrincipal;
            SiteIdentity si = sp.Identity as SiteIdentity;
            TextBoxCreator.Text = si.FullName;
        }

        protected void ModifyContest(string ContestId)
        {
            BoContests Contest = ContestService.getContest(ContestId);

            CON_NAMEBOX.Value = Contest.CON_ID;
            TextBoxName.Text = Contest.CON_NAME;

            CompetitionList.SelectedValue = Contest.CON_COMPETITIONS;

            TextBoxCreator.Text = Contest.CON_CREATOR;
            TextBoxYear.Text = Contest.CON_YEAR.ToString();
            TextBoxMonth.Text = Contest.CON_MONTH.ToString();
            TextBoxADDRESS.Text = Contest.CON_ADDRESS;
            TextBoxTEACHERS.Text = Contest.CON_TEACHERS;
            KindEditor.Text = Contest.CON_CONTENTS;

        }

        protected void DeleteContest(string ContestId)
        {
            BoContests contest = new BoContests();
            contest.CON_ID = ContestId;
            ContestService.DeleteContest(contest);
            Response.Redirect("OutterReport.aspx");
        }

        protected void ButtonOK_Click(object sender, EventArgs e)
        {
            //BoCompetitions compatation = new BoCompetitions();
            //compatation.COM_TYPE = 1;
            if (CON_NAMEBOX.Value == "")
            {
                BoContests Contests = new BoContests();
                Contests.CON_NAME = TextBoxName.Text;
                Contests.CON_COMPETITIONS = CompetitionList.Text;
                Contests.CON_CREATOR = TextBoxCreator.Text;
                Contests.CON_YEAR = Convert.ToInt32(TextBoxYear.Text);
                Contests.CON_MONTH = Convert.ToInt32(TextBoxMonth.Text);
                Contests.CON_ADDRESS = TextBoxADDRESS.Text;
                Contests.CON_TEACHERS = TextBoxTEACHERS.Text;
                Contests.CON_CONTENTS = KindEditor.Text;
                ContestService.CreateContest(Contests);
                Response.Redirect("OutterReport.aspx");
            }
            else
            {
                BoContests Contests = ContestService.getContest(CON_NAMEBOX.Value);
                Contests.CON_ID = CON_NAMEBOX.Value;
                Contests.CON_NAME = TextBoxName.Text;
                Contests.CON_COMPETITIONS = CompetitionList.SelectedValue;
                Contests.CON_CREATOR = TextBoxCreator.Text;
                Contests.CON_YEAR = Convert.ToInt32(TextBoxYear.Text);
                Contests.CON_MONTH = Convert.ToInt32(TextBoxMonth.Text);
                Contests.CON_ADDRESS = TextBoxADDRESS.Text;
                Contests.CON_TEACHERS = TextBoxTEACHERS.Text;
                Contests.CON_CONTENTS = KindEditor.Text;

                ContestService.UpdateContest(Contests);
                Response.Redirect("OutterReport.aspx");
            }
        }
    }
}